# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@rules_cc//cc:defs.bzl", "cc_library")
load("//bazel:toolchain_transitions.bzl", "cc_clang_binary")

package(default_visibility = ["//src/stirling:__subpackages__"])

# We build test_exe with clang regardless of the bazel config.
# This is test data, so we want deterministic debug symbols.
cc_clang_binary(
    name = "test_exe",
    srcs = ["test_exe.cc"],
    copts = [
        "-O0",
        "-g",
    ],
    features = [
        # disable the default optimization flags so we can set our own for this target.
        "-default_opt_level_flags",
        # disable sanitiziers so that the binary is deterministic regardless of bazel config.
        "-asan",
        "-msan",
        "-tsan",
    ],
    linkopts = [
        "-Wl,--build-id",
    ],
)

cc_library(
    name = "test_exe_fixture",
    hdrs = ["test_exe_fixture.h"],
    data = [":test_exe"],
    deps = ["//src/common/exec:cc_library"],
)

filegroup(
    name = "stripped_exe",
    srcs = [
        "stripped_test_exe",
        "usr/lib/debug/.build-id/7d/eb0e3f89deba61.debug",
    ],
)

filegroup(
    name = "prebuilt_exe",
    srcs = [
        "prebuilt_test_exe",
    ],
)

#------------------------------------------------------------------------------

# The output of test_exe_stripped_target is also checked into the repo as stripped_test_exe.
# This is because we need a consistent build-id for testing purposes.

genrule(
    name = "test_exe_stripped_target",
    srcs = [":test_exe"],
    outs = [
        "test_exe_stripped",
    ],
    cmd = "$(OBJCOPY) --strip-all $(location :test_exe) $(location test_exe_stripped)",
    toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
)

genrule(
    name = "test_exe_debug_target",
    srcs = [":test_exe"],
    outs = [
        "test_exe.debug",
    ],
    cmd = "$(OBJCOPY) --only-keep-debug $(location :test_exe) $(location test_exe.debug)",
    toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
)

genrule(
    name = "test_exe_debuglink_target",
    srcs = [
        ":test_exe",
        ":test_exe_debug_target",
    ],
    outs = [
        "test_exe_debuglink",
    ],
    cmd = "$(OBJCOPY) --strip-all --add-gnu-debuglink=$(location :test_exe_debug_target) $(location :test_exe) $(location test_exe_debuglink)",
    toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
)

filegroup(
    name = "lib_foo",
    srcs = ["lib_foo_so"],
)
